﻿/*****************************************************
 * 本类库的核心系 JNTemplate
 * (C) JNTemplate 2011
 * 作者：翅膀的初衷 QQ:4585839
 * 网址：http://www.JiNianNet.com
 *****************************************************/

using System;
using System.Collections.Generic;
using System.Text;

namespace JinianNet.JNTemplate
{
    public class BuildManager
    {
        private static EngineCollection _engines = new EngineCollection();

        public static EngineCollection Engines
        {
            get {
                if (_engines.Count == 0)
                {
                    _engines.Add(new Engine());
                }
                return _engines;
            }
        }

        public static ITemplate CreateTemplate(string path)
        {
            ITemplate template = null;
            foreach (IEngine engine in Engines)
            {
                template = engine.CreateTemplate();
                if (template != null)
                {
                    break;
                }
            }
            if (template == null)
            {
                throw new ArgumentException("ITemplate");
            }
            if (JinianNet.JNTemplate.Process.Common.FileHelper.IsVirtualPath(path))
                path = JinianNet.JNTemplate.Process.Common.FileHelper.ToRealPath(path, template.Context.CurrentPath);
            if (System.IO.File.Exists(path))
            {
                template.Context.CurrentPath = System.IO.Path.GetDirectoryName(path);
                template.TemplateContent = JinianNet.JNTemplate.Process.Common.FileHelper.Load(path, template.Context.Charset);
            }
            else
            {
                template.TemplateContent = "Template Not Found";
            }
            return template;

        }
    }
}
